package com.fkc.javahansoulbms.controller;


import com.fkc.javahansoulbms.entity.Admin;
import com.fkc.javahansoulbms.exception.ComFoundException;
import com.fkc.javahansoulbms.service.AdminService;
import com.fkc.javahansoulbms.util.JwtUtils;
import com.fkc.javahansoulbms.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员控制类
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@PostMapping("/login")
	public Result login(@RequestBody Admin admin) {
		try {
			Admin loggedInAdmin = adminService.loginAdmin(admin.getAdminAccount(), admin.getAdminPassword());
			
			if (loggedInAdmin != null) {
				String token = JwtUtils.generateToken(loggedInAdmin.getAdminAccount());
				
				return Result.ok().data("token", token).data("admin", loggedInAdmin);
			}else {
				return Result.error().message("账号或密码错误");
			}
		} catch (ComFoundException e) {
			return Result.error().message(e.getMessage());
		}
	}
}
